package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ChatRoom;
import cn.wolfcode.trip.base.domain.ChatRoomState;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.ChatRoomMapper;
import cn.wolfcode.trip.base.mapper.ChatRoomStateMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.IChatRoomService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class ChatRoomServiceImpl implements IChatRoomService {

    @Autowired
    private ChatRoomMapper chatRoomMapper;

    @Autowired
    private ChatRoomStateMapper stateMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectUsersByChatRoomId(Long chatRoomId) {
        return chatRoomMapper.selectUsersByChatRoomId(chatRoomId);
    }

    @Override
    public List<ChatRoom> selectRoomName(Long roomId) {
        return chatRoomMapper.selectRoomName(roomId);
    }

    @Override
    public void insert(ChatRoom chatRoom) {
        chatRoom.setSend(UserContext.getCurrentUser());
        chatRoom.setSendState(1);
        chatRoomMapper.insert(chatRoom);
        //当前聊天室所有的用户
        List<User> cs = chatRoomMapper.selectUsersByRoomId(chatRoom.getRoom());
        System.out.println(cs.size());
        for (User c : cs) {
            //遍历,把当前登录用户排除掉
            if (c.getId() != UserContext.getCurrentUser().getId()) {
                ChatRoomState chatRoomState = new ChatRoomState();
                chatRoomState.setRoom(chatRoom.getRoom());
                chatRoomState.setState(0);
                chatRoomState.setOtherUser(c);
                chatRoomState.setChatroom(chatRoom);
                stateMapper.insert(chatRoomState);
            }
        }
    }

    @Override
    public List<ChatRoom> selectNewMessage(Long roomId, Long userId) {
        List<ChatRoom> list = chatRoomMapper.selectNewMessage(roomId, userId);
        return list;
    }

    @Override
    public void chanegSendState(Long id, Long uId) {
        chatRoomMapper.chanegSendState(id, uId);
    }

    //创建聊天室
    @Override
    public void createRoom(String[] str, String roomName) {
        Long timeMillis = System.currentTimeMillis();
        System.out.println(timeMillis);
        String room = timeMillis.toString().substring(5, 13);
        for (String s : str) {
            User user = userMapper.selectByPrimaryKey(Long.parseLong(s));
            ChatRoom chatRoom = new ChatRoom(user, null, "我是" + user.getNickName(), 1, Long.parseLong(room), roomName);
            chatRoomMapper.createRoom(chatRoom);
        stateMapper.insertRelation(Long.parseLong(room),user.getId());
        }
    }

    @Override
    public List<ChatRoom> selectRoomUsersByLoginId(Long loginId) {
        List<ChatRoom> chatRoom = chatRoomMapper.selectByLoginId(loginId);
        return chatRoom;
    }

    @Override
    public List<User> selectRoomUsersByRoomId(Long roomId) {
        return chatRoomMapper.selectUsersByRoomId(roomId);
    }
}
